package com.pomelo.tiku.activities

import android.os.Bundle
import android.os.PersistableBundle
import android.os.Process
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.LogUtils
import com.pomelo.tiku.R
import com.pomelo.tiku.frgs.BaseFragment
import com.pomelo.tiku.frgs.FragmentHome
import com.pomelo.tiku.frgs.FragmentQuestion
import com.pomelo.tiku.frgs.FragmentUser
import com.pomelo.tiku.managers.SharedPreferenceManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.collections.contains as contains1


class MainActivity : BaseActivity(), View.OnClickListener {
    private var mFrgHome:FragmentHome? = null
    private var mFrgQuestion: FragmentQuestion? = null
    private var mFrgUser: FragmentUser? = null

    private var mListFrg: ArrayList<Fragment>? = null
    private var mCurPosition: Int = 0
    private var pos = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        initFragments()
        tab_home.setOnClickListener(this)
        tab_question.setOnClickListener(this)
        tab_user.setOnClickListener(this)
    }

    fun initFragments() {
        mFrgHome = FragmentHome()
        mFrgQuestion = FragmentQuestion()
        mFrgUser = FragmentUser()

        mListFrg = ArrayList()
        mListFrg!!.add(mFrgHome!!)
        mListFrg!!.add(mFrgQuestion!!)
        mListFrg!!.add(mFrgUser!!)
        changeToPostion(pos)
    }

    private fun changeToPostion(position: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        mCurPosition = position
        mListFrg?.get(position)?.let {
            transaction.replace(
                R.id.home_frameLayout,
                it,
                "mainactivity"
            )
        }
        transaction.commit()
    }

    override fun onClick(view: View?) {
        when(view!!.id) {
            R.id.tab_home -> {
                changeToPostion(0)
            }
            R.id.tab_question-> {
                changeToPostion(1)
            }
            R.id.tab_user -> {
               changeToPostion(2)
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
       super.onSaveInstanceState(outState, outPersistentState)

    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)

    }

    override fun onDestroy() {
        super.onDestroy()
        mFrgHome = null
        mFrgQuestion = null
        mFrgUser = null
    }

    /*重新加载布局*/
    fun reLoadFragView() {
        /*现将该fragment从fragmentList移除*/
        if (mListFrg!!.contains1(mFrgUser as Fragment)) {
            mListFrg!!.remove(mFrgUser as Fragment)
        }
        /*从FragmentManager中移除*/
        supportFragmentManager.beginTransaction().remove(mFrgUser!!)
            .commit()

        /*从新建立*/
        mFrgUser = FragmentUser()
        /*添加到fragmentList*/
        mListFrg!!.add(mFrgUser!!)

        /*显示*/
        changeToPostion(2)
    }

}